package com.service.impl;

import com.dao.UserMapper;
import com.github.pagehelper.PageInfo;
import com.model.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    //根据用户id，查询用户的当前可借阅数
    @Override
    public int queryBorrowNumberByUserId(int userid) {
        User user = userMapper.selectByPrimaryKey(userid);
        System.out.println(user);
        int borrowNumber=user.getBorrowNumber();
        return borrowNumber;
    }
    //借书成功后，当前用户可借阅数减一
    @Override
    public int updateBorrowNumber(int userid,int borrowNumber) {
        User user=new User();
        user.setUserid(userid);
        user.setBorrowNumber(borrowNumber-1);
        int i = userMapper.updateByPrimaryKeySelective(user);
        return i;
    }






    //汪林

    //id查询
    @Override
    public User queryUserByUserId(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    //修改
    @Override
    public int updateByPrimaryKeySelective(User user_db) {
        return userMapper.updateByPrimaryKeySelective(user_db);
    }


    //查询所有的用户基本信息
    @Override
    public HashMap<String,Object> queryAllUser(User user, String page, String limit) {

        List<User> userList=userMapper.selectAll();
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }
    @Override
    public void delectUserByUserId(int userId) {
        userMapper.deleteByPrimaryKey(userId);
    }
    //更改用户的部分信息
    @Override
    public void updateUser(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }
}
